package practice.test3.studentRecord.plan1;

import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class ScoreReducer extends Reducer<Text,Text,NullWritable,StuBean> {



    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {

        StuBean stuBean = new StuBean();
        double sum = 0;
        for(Text value : values) {
            double score = 0;

            //`\\`进行字符转义
            String[] split = value.toString().split("\\|");
            if (split.length != 3) {
                return;
            }
            stuBean.setStuName(split[1]);
            score = Double.parseDouble(split[2]);

            if (split[0].equals("a")) {
                stuBean.setChScore(split[2]);

            } else if (split[0].equals("b")) {

                stuBean.setEngScore(split[2]);

            } else if (split[0].equals("c")) {
                stuBean.setMathScore(split[2]);
            }

            sum += score;
        }
        stuBean.setTotal(sum+"");
        stuBean.setAverage(sum/3 + "");

        context.write(NullWritable.get(),stuBean);

    }
}
